package org.xserver.common.util;

import java.nio.charset.Charset;

/**
 * Responsible for Encode and Decode work
 * 
 * @author idoldefine
 * @since 2013/01/08
 */
public class CharsetUtil {
	public static final Charset GBK = Charset.forName("GBK");
	public static final Charset GB2312 = Charset.forName("GB2312");
	public static final Charset GB18030 = Charset.forName("GB18030");
	public static final Charset UTF_8 = Charset.forName("UTF-8");
	public static final Charset UTF_16 = Charset.forName("UTF_16");
	public static final Charset UTF_16BE = Charset.forName("UTF_16BE");
	public static final Charset UTF_16LE = Charset.forName("UTF_16LE");
	public static final Charset US_ASCII = Charset.forName("US-ASCII");
	public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");

	public static final String[] LOWER_CHARACTER = lowerCharacter();
	public static final String[] UPPER_CHARACTER = upperCharacter();
	public static final String[] CHARACTER = character();
	public static final String[] CHARACTER_NUMBER = characterNumber();
	public static final String[] NUMBER = new String[] { "0", "1", "2", "3",
			"4", "5", "6", "7", "8", "9" };

	public static String newString(byte[] bytes, Charset charset) {
		if (bytes == null) {
			return null;
		}

		return new String(bytes, charset);
	}

	public static byte[] getBytes(String content, Charset charset) {
		return content.getBytes(charset);
	}

	private static String[] lowerCharacter() {
		String[] lowerCharacters = new String[26];
		for (int i = 65, j = 0; i <= 90; i++, j++) {
			lowerCharacters[j] = String.valueOf((char) i);
		}

		return lowerCharacters;
	}

	private static String[] upperCharacter() {
		String[] upperCharacters = new String[26];
		for (int i = 97, j = 0; i <= 122; i++, j++) {
			upperCharacters[j] = String.valueOf((char) i);
		}

		return upperCharacters;
	}

	private static String[] character() {
		String[] characters = new String[26 * 2];
		for (int i = 65, j = 0; i <= 90; i++, j++) {
			characters[j] = String.valueOf((char) i);
		}

		for (int i = 97, j = 26; i <= 122; i++, j++) {
			characters[j] = String.valueOf((char) i);
		}

		return characters;
	}

	private static String[] characterNumber() {
		String[] characters = new String[26 * 2 + 10];
		for (int i = 65, j = 0; i <= 90; i++, j++) {
			characters[j] = String.valueOf((char) i);
		}

		for (int i = 97, j = 26; i <= 122; i++, j++) {
			characters[j] = String.valueOf((char) i);
		}

		for (int i = 0, j = 52; i <= 9; i++, j++) {
			characters[j] = String.valueOf(i);
		}
		return characters;
	}
}
